/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package stats;

import java.util.*;


import javax.servlet.jsp.tagext.SimpleTagSupport;
import javax.servlet.jsp.JspException;
import java.io.*;

/**
 *
 * @author balbir kaur
 */
public class UserData extends SimpleTagSupport {

    boolean flagmain = false;
    ArrayList<Long> appUsers = null;
    int count = 0;
    int friendCount = 0;
    Double percentage;
    Long uid = null;

    @Override
    public void doTag() throws JspException, IOException {
        this.checkUserId();
        if (flagmain) {
            updateUser();
        } else {
            insertUser();
        }
        String temp=getScore().toString();
        getJspContext().getOut().write(temp);
        getJspContext().setAttribute("userrank", temp);


    }

    public void setUid(Long id) {
        this.uid = id;
    }

    public void setAppUsers(ArrayList<Long> friendid) {
        this.appUsers = friendid;
    }

    public void setPercentage(Double percentage) {
        this.percentage = percentage;
    }

    public void checkUserId() {
        try {
              if (uid != null) {
                FacebookDatabase fbdb = new FacebookDatabase();
                FacebookDatabase.initialize();
                fbdb.openConnection();
                flagmain = fbdb.checkUser(uid);
                fbdb.closeConnection();
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void insertUser() {

        FacebookDatabase fbdb = new FacebookDatabase();
        fbdb.openConnection();
        fbdb.insertUser(uid, percentage);
        fbdb.closeConnection();

    }

    public void updateUser() {

        FacebookDatabase fbdb = new FacebookDatabase();
        fbdb.openConnection();
        fbdb.updateUser(uid, percentage);
        fbdb.closeConnection();


    }

    public ArrayList<Long> checkFriendsInTable() {
        ArrayList<Long> templ = new ArrayList<Long>();
        if (appUsers != null) {

            Iterator it = appUsers.iterator();
            Long temp;
            boolean flag;
            while (it.hasNext()) {
                temp = (Long) it.next();
                FacebookDatabase fbdb = new FacebookDatabase();
                fbdb.openConnection();
                flag = fbdb.checkUser(temp);
                fbdb.closeConnection();
                if (flag) {
                    templ.add(temp);
                }
                appUsers = templ;
                friendCount = templ.size();

            }
        }
        return templ;

    }

    public Double getScore() {
        TreeMap m = new TreeMap();
        FacebookDatabase fbdb = new FacebookDatabase();
        fbdb.openConnection();
        Double score_u = fbdb.getUserPercentage(uid);
        fbdb.closeConnection();
        ArrayList<Long> ali = this.checkFriendsInTable();
        Iterator it = ali.iterator();
        Double score_f = null;
        while (it.hasNext()) {
            fbdb.openConnection();
            Long temp;
            temp = (Long) it.next();
            score_f = fbdb.getUserPercentage(temp);
            fbdb.closeConnection();
            if (score_f > score_u) {
                count = count + 1;
            }

        }
        if(count<friendCount){
        Double posn = ((friendCount - count) / (friendCount * 1.0)) * 100;
        return posn;
        }
        else return 0.00;
    }
}
